#!/bin/bash

function config_fedora_yum(){
	echo "Start configuring fedora33 yum source, please wait..."
	> /etc/yum.repos.d/openEuler.repo
	cat > /etc/yum.repos.d/openEuler.repo << END_FC
[fedora-33]
name=fedora-33
baseurl=http://mirrors.aliyun.com/fedora/releases/33/Everything/aarch64/os/
enabled=1
gpgcheck=0

[fedora-33-src]
name=fedora-33-src
baseurl=http://mirrors.aliyun.com/fedora/releases/33/Everything/source/tree/
enabled=1
gpgcheck=0
END_FC
	yum makecache >/dev/null
	if [ $? -eq 0 ];then
		echo "Configure fedora yum source succeed !!!"
	else
		echo "Configure fedora yum source failed !!!"
		exit 1
	fi
}

# According to the failed_log file find which src rpmpkg provide it.
function search_src_rpm(){
	if [ ! -s /root/check_result/failed_install_pkglist ];then
		echo "All pkg install and uninstall without no problems !!!"
		exit 0
	fi
	config_fedora_yum
	echo "Searching source pkg is doing, please wait..."
	failed_log_path="/root/check_result/failed_log"
	require_path="/root/check_result/failed_pkg_require"
	mkdir -p ${require_path}
	for pkg in `cat /root/check_result/failed_install_pkglist`
	do
		echo "Search ${pkg} requirements is doing..."
		if [ ! -s ${require_path}/nothing_provides_source_rpm${pkg} ];then
			touch ${require_path}/nothing_provides_source_rpm_${pkg}
		fi		
		if [[ "x`cat ${require_path}/nothing_provides_source_rpm_${pkg} | grep "===${pkg} require list==="`" == "x" ]];then
			echo "===${pkg} require list===" >> ${require_path}/nothing_provides_source_rpm_${pkg}
		fi
		cat ${failed_log_path}/${pkg}/${pkg}_log | grep "nothing provides" > ${failed_log_path}/${pkg}/nothing_provides_log
		sed -i 's/  - nothing provides //g' ${failed_log_path}/${pkg}/nothing_provides_log
		cat ${failed_log_path}/${pkg}/nothing_provides_log | while read line
		do
			name=${line% needed*}
			dnf repoquery --whatprovides "$name" &>/dev/null > res
			wpname=$(cat res)
			# return None
			if [[ -z $wpname ]];then
				echo "Source       : $name" >> ${require_path}/nothing_provides_source_rpm_${pkg}
			else
				#according binary pkg to Find source pkgname
				dnf repoquery -i "${wpname%%-[0-9]*}" 2>/dev/null | grep Source | head -1 >> ${require_path}/nothing_provides_source_rpm_${pkg}
			fi
		done
		rm -f res
	done
	cat ${require_path}/nothing_provides_source_rpm_* > ${require_path}/nothing_provides_source_rpm 2>/dev/null
	rm -f ${require_path}/nothing_provides_source_rpm_*
	echo "Searching source pkg have done !!!"
}

function check_install(){
	# config yum
#	echo "Start configuring openEuler yum source, please wait..."
#	cat > /etc/yum.repos.d/openEuler.repo << END_FILE
#[SP2]
#name=SP2
#baseurl=http://119.3.219.20:82/openEuler:/20.03:/LTS:/SP2/standard_aarch64
#enabled=1
#gpgcheck=0
#END_FILE
#	yum makecache >/dev/null
#	if [ $? -eq 0 ];then
#		echo "Configure openEuler yum source succeed !!!"
#	else
#		echo "Configure openEuler yum source failed !!!"
#		exit 1
#	fi
	
	# check pkg install and uninstall
	pkglist=$1
	cd /root/
	mkdir -p /root/check_result/success_log
	mkdir -p /root/check_result/failed_log
	echo "Checking pkg install and uninstall is doing, please wait..."
	for pkg in $(cat /root/${pkglist})
	do
		echo "${pkg} is installing..."
		yum localinstall -y /root/all_binary/$pkg/*.rpm > install_res_${pkg} 2>&1
		# unset pkg binary rpmlist
		unset rpmlist

		# get install rpm binary list
		cd /root/all_binary/$pkg/
		for rpm in `ls *.rpm`
		do
			rpm=${rpm%.rpm}
			rpmlist="$rpmlist $rpm"
		done
		cd /root/
		# verify
		if [ `cat install_res_${pkg} | grep "Complete!"` == "Complete!" ] &>/dev/null;then
			echo "${pkg} is install successful !!!"
			echo "$pkg" > /root/check_result/${pkg}_success_install
			if [ ! -d /root/check_result/success_log/${pkg} ];then
				mkdir -p /root/check_result/success_log/${pkg}
			fi
			mv /root/install_res_${pkg} /root/check_result/success_log/${pkg}/${pkg}_install

			# function verify
			echo "verifying ${pkg} function..."
			function_list=$(rpm -ql /root/all_binary/$pkg/*.rpm | grep -E '/usr/s?bin')
			for function in $function_list
			do
				timeout 5 $function --help &>/dev/null
				if [ $? -eq 0 ];then
					echo "verifying $function succeed!"
				else
					echo "verifying $function failed!"
					echo "$pkg [$function]" >> failed_function_list_${pkg}
				fi
			done
			if [ -f "failed_function_list_${pkg}" ];then
				mkdir -p /root/check_result/failed_log/${pkg}
				mv failed_function_list_${pkg} /root/check_result/failed_log/${pkg}/
				echo "$pkg" > /root/check_result/function_failed_${pkg}
			else
				echo "$pkg" > /root/check_result/function_succeed_${pkg}
			fi
			
			# uninstall
			echo "${pkg} is uninstalling..."
			#yum remove $rpmlist -y > uninstall_res 2>&1
			yum history undo `yum history list | sed -n '3p' | awk -F '|' '{print $1}'` -y > uninstall_res_${pkg} 2>&1
			if [ `cat uninstall_res_${pkg} | grep "Complete!"` == "Complete!" ] &>/dev/null;then
				echo "${pkg} is uninstall successful !!!"
				echo "$pkg" > /root/check_result/${pkg}_success_uninstall
	 			mv uninstall_res_${pkg} /root/check_result/success_log/${pkg}/${pkg}_uninstall
			else
				echo "${pkg} is uninstall failed !!!"
				echo "$pkg" > /root/check_result/${pkg}_failed_uninstall
				if [ ! -d /root/check_result/failed_log/${pkg} ];then
					mkdir -p /root/check_result/failed_log/${pkg}
				fi
				mv uninstall_res_${pkg} /root/check_result/failed_log/${pkg}/${pkg}_uninstall
			fi
			rm -f uninstall_res_${pkg}
		else
			echo "${pkg} is install failed !!!"
			echo "$pkg" > /root/check_result/${pkg}_failed_install
			
			if [ ! -d /root/check_result/failed_log/${pkg} ];then
				mkdir -p /root/check_result/failed_log/${pkg}
			fi
	 		mv install_res_${pkg} /root/check_result/failed_log/${pkg}/${pkg}_log
	 	fi
		rm -f install_res_${pkg}
	done
	echo "Checking all pkg install and uninstall have done !!!"
}

check_install $1 
#search_src_rpm
